package com.yx.changdao.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yx.changdao.common.annotation.AutoLog;
import com.yx.changdao.common.data.Result;
import com.yx.changdao.common.entity.DepWorker;
import com.yx.changdao.common.entity.ProblemInfo;
import com.yx.changdao.common.entity.SysUser;
import com.yx.changdao.common.exception.AlertException;
import com.yx.changdao.common.utils.Faster;
import com.yx.changdao.common.utils.SpringContextUtils;
import com.yx.changdao.common.utils.SysUtils;
import com.yx.changdao.common.utils.UploadUtils;
import com.yx.changdao.service.DepWorkerService;
import com.yx.changdao.service.ProblemInfoService;
import com.yx.changdao.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class CommonController {

    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private ProblemInfoService problemInfoService;
    @Autowired
    private DepWorkerService depWorkerService;
    @AutoLog
    @PostMapping("/img")
    /*@RequiresPermissions("upload:img")
    @ShiroDesc("上传:图片")*/
    public Result img(@RequestParam("imgs") List<MultipartFile> mus) throws AlertException {

        return Result.ok().data(UploadUtils.batchUploadImage(mus));
    }

    @AutoLog
    @PostMapping("/file")
    /*@RequiresPermissions("upload:file")
    @ShiroDesc("上传:文件")*/
    public Result file(@RequestParam("files") List<MultipartFile> mus) throws AlertException {

        return Result.ok().data(UploadUtils.batchUploadFile(mus));
    }

    @AutoLog
    @GetMapping("/refreshToken")
    public Result refreshToken() throws AlertException {
        SysUser sysUser = SysUtils.currentUser();
        if (sysUser == null) {
            return Result.error("未登录");
        }
        String newToken = this.sysUserService.generateJwtToken(sysUser);
        Map<String, Object> map = new HashMap<>();
        Long time = SpringContextUtils.getAppConfig("jwt.time", Long.class, -1L);
        map.put("authToken", newToken);
        map.put("expires_time", System.currentTimeMillis() + time * 1000L);//这里是毫秒
        return Result.ok().data(map);
    }

    /**
     * 根据问题信息id查询反馈居民身份证号
     * @param problemInfoId
     * @return
     * @throws AlertException
     */
    @AutoLog
    @GetMapping("/problem/residentIdcard")
    public Result residentIdcard(@RequestParam("problemInfoId") String problemInfoId) throws AlertException {
        ProblemInfo problemInfo = problemInfoService.getOne(new QueryWrapper<ProblemInfo>().eq(ProblemInfo.PROBLEM_INFO_ID, problemInfoId));
        if(Faster.isNull(problemInfo)){
            throw new AlertException("未查到问题信息");
        }
        Map<String, String> map = new HashMap<>();
        map.put("residentIdcard",problemInfo.getResidentIdcard());
        return Result.ok().data(map);
    }

    /**
     * 部门工作人员id查询身份证号
     * @param depWorkerId
     * @return
     * @throws AlertException
     */
    @AutoLog
    @GetMapping("/depWorker/workerIdcard")
    public Result depWorkerId(@RequestParam("depWorkerId") String depWorkerId) throws AlertException {
        DepWorker depWorker = depWorkerService.getOne(new QueryWrapper<DepWorker>().eq(DepWorker.DEP_WORKER_ID, depWorkerId));
        if(Faster.isNull(depWorker)){
            throw new AlertException("未查到人员信息");
        }
        Map<String, String> map = new HashMap<>();
        map.put("workerIdcard",depWorker.getWorkerIdcard());
        return Result.ok().data(map);
    }
}
